import {Store} from 'iflux2';
import webApi from '../receipt-common/webApi';
import {fromJS} from 'immutable';
import OrderActor from './actor/order-actor';
import {OrderActorType} from './constant/action-type';

import {message} from 'antd';

export default class AppStore extends Store {
    bindActor() {
        return [
            new OrderActor
        ]
    }


    queryOrderDetail = async (tid = "")=>{
        try {
            const {orderStatusEnum, tradeOrder} = await webApi.queryOrderDetail(tid);
            this.dispatch(OrderActorType.SET_ORDER, fromJS(tradeOrder));
            this.dispatch(OrderActorType.SET_ORDER_STATUS, orderStatusEnum);
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    receipt = async (tid = "")=>{
        try {
            await webApi.receipt(tid);
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    completeOrder = async(tid = "")=>{
        try {
            await webApi.completeOrder(tid);
        } catch (err) {
            return Promise.reject(err);
        }
    }


    setOrderStatus = (orderStatus)=>{
        this.dispatch(OrderActorType.SET_ORDER_STATUS, orderStatus);
    }
}